package top.retain;

import java.util.LinkedHashMap;
import java.util.Stack;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class Test{

    public String decodeString(String s) {
        LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();

        StringBuilder res = new StringBuilder();
        Stack<Character> stack = new Stack();
        for (int i = s.length() - 1;i >= 0; i--) {
            stack.push(s.charAt(i));
        }
        while(!stack.isEmpty()) {
            char c = stack.pop();
            // 无规则
            if (!Character.isDigit(c) && c != '[' && c != ']') {
                res.append(c);
            }
            if (Character.isDigit(c)) {
                int count = (int)c - (int) '0';
                while (Character.isDigit(stack.peek())) {
                    count += (int) stack.pop() - (int) '0';
                }
                // 遇见[
                stack.pop();
                // 记录括号内容
                StringBuilder sub = new StringBuilder();
                char nextPop;
                while (']' != (nextPop = stack.pop())){
                    sub.append(nextPop);
                }
                // 开始拼接
                while (count > 0) {
                    res.append(sub);
                }
            }
        }
        return res.toString();
    }

    public static void main(String[] args) throws InterruptedException {

        int count = '1';
        System.out.println(count);
        CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).whenComplete((v, t) -> System.out.println("done" + v.toString()));
        Thread.currentThread().join();
    }

}
